package com.warm.androidstudy.utils;

import android.content.Context;
import android.text.TextUtils;

import androidx.annotation.NonNull;


import com.warm.androidstudy.MyApplication;
import com.warm.androidstudy.config.AppConfig;
import com.warm.androidstudy.constant.SConstant;

import java.util.Random;
import java.util.UUID;


/**
 * SP工具类（继承 Common）
 * by admin on 2017/8/15.
 */
public class AppSPUtils {


    /**
     * 取得LruCache/SP文件里存储的变量值
     *
     * @param key
     * @return
     */
    public static Object getSpConfigValue(@NonNull String key, @NonNull Object defaultValue) {
        if (TextUtils.isEmpty(key)) {//忽略空key
            return null;
        }
        return SPUtils.get(MyApplication.getInstance().getApplicationContext(), AppConfig.SP_FILE_NAME, key, defaultValue);
    }

    /**
     * 设置LruCache/SP文件里存储的变量值
     *
     * @param key
     * @param value
     */
    public static void putSpConfigValue(String key, Object value) {
        if (value == null) {//忽略空key或者空value
            return;
        }
        if (SPUtils.contains(MyApplication.getInstance().getApplicationContext(), AppConfig.SP_FILE_NAME, key)) {
            SPUtils.remove(MyApplication.getInstance().getApplicationContext(), AppConfig.SP_FILE_NAME, key);
        }
        SPUtils.put(MyApplication.getInstance().getApplicationContext(), AppConfig.SP_FILE_NAME, key, value);
    }

    /**
     * 取得用户数据库名
     *
     * @return
     */
    public static String getUserDbName() {
        return (String) AppSPUtils.getSpConfigValue(SConstant.SP_KEY_USER_DB_NAME, "");
    }

    /**
     * 平台token是否有效
     * 默认无效
     *
     * @return
     */
    public static boolean isTokenValid() {
        String validValue = (String) AppSPUtils.getSpConfigValue(SConstant.SP_KEY_TOKEN_VALID, "0");
        return !"0".equals(validValue);
    }



    /**
     * 清除sessionId（登录时触发）
     */
    public static void ToClearSessionId(Context context) {
        SPUtils.put(MyApplication.getInstance(), SConstant.LoginFile_UUID, SConstant.Key_LoginFile_UUID, "");
        SPUtils.clear(context, SConstant.LoginFile_UUID);
        String currentUID = AppSPUtils.getCurrentUID();
        if (TextUtils.isEmpty(currentUID)) {
            LogUtil.e("-------session已清空");
        }
    }

    //唯一ID uuid生成，清数据后重新生成（登录时触发）
    public static String getNewUUID() {
        String uid = UUID.randomUUID().toString();
        LogUtil.e("-------session重新创建成功！:" + uid);
        SPUtils.put(MyApplication.getInstance(), SConstant.LoginFile_UUID, SConstant.Key_LoginFile_UUID, uid);
        return uid;
    }

    /**
     * 获取当前sessionId值，判断是否登录状态
     *
     * @return
     */
    public static String getCurrentUID() {
        String uid = (String) SPUtils.get(MyApplication.getInstance(), SConstant.LoginFile_UUID, SConstant.Key_LoginFile_UUID, "");
        if (StringUtils.isEmptyWithNullStr(uid)) {
            uid = "";
        }
        return uid;
    }

    public static String getRandomString(int length) {
        //length表示生成字符串的长度
        String base = "abcdefghijklmnopqrstuvwxyz0123456789";
        Random random = new Random();
        StringBuffer sb = new StringBuffer();
        for (int i = 0; i < length; i++) {
            int number = random.nextInt(base.length());
            sb.append(base.charAt(number));
        }
        return sb.toString();
    }
}
